//
//  WNChapterVC.swift
//  WalkNovel
//
//  Created by Kevin on 2023/7/12.
//

import UIKit
import HandyJSON

class WNChapterVC: WNBaseVC {
    
    lazy var chapterView: ChapterView = {
        return ChapterView()
    }()
    
    lazy var chapterTabView: WNTableView = {
        let tab = WNTableView()
        tab.register(ChapterCell.self, forCellReuseIdentifier: ChapterCell.identifier)
        tab.delegate = self
        tab.dataSource = self
        tab.rowHeight = 48
        return tab
    }()
            
    var bookDetailModel: BookDetailModel? {
        didSet {
            chapterView.model = bookDetailModel
        }
    }

    var chapterArr = [ChapterDetailModel]()
    
    var isRead = false
        
    var chapterIdClosure: IntClosure?

    override func viewDidLoad() {
        super.viewDidLoad()
    }
    
    override func viewConfig() {
        wn_hidNavView()
        view.addSubview(chapterView)
        view.addSubview(chapterTabView)
        chapterView.snp.makeConstraints { make in
            make.top.equalTo(StatusBarHeight)
            make.left.right.equalTo(0)
            make.height.equalTo(scaleWidth(90)+25)
        }
        chapterTabView.snp.makeConstraints { make in
            make.top.equalTo(chapterView.snp.bottom)
            make.left.right.equalTo(0)
            make.bottom.equalTo(-TabBarMargin)
        }
    }
    
    override func dataConfig() {
        pageName = "章節列表"
        getChapterListRequest()
        if isRead {
            darkModeConfig()
            chapterView.darkModeConfig()
        }
    }
    
    func darkModeConfig() {
        view.backgroundColor = isDark() ? .init(named: "black_222222") : .white
        chapterTabView.backgroundColor = isDark() ? .init(named: "black_222222") : .white
    }
}

extension WNChapterVC: UITableViewDelegate, UITableViewDataSource {
        
    func tableView(_ tableView: UITableView, numberOfRowsInSection section: Int) -> Int {
        return chapterArr.count
    }
    
    func tableView(_ tableView: UITableView, cellForRowAt indexPath: IndexPath) -> UITableViewCell {
        let cell = tableView.dequeueReusableCell(withIdentifier: ChapterCell.identifier, for: indexPath) as! ChapterCell
        cell.tag = indexPath.item+1
        cell.durChapterId = bookDetailModel?.durChapterId ?? 0
        cell.model = chapterArr[indexPath.item]
        if isRead {
            cell.darkModeConfig()
        }
        return cell
    }
    
    func tableView(_ tableView: UITableView, didSelectRowAt indexPath: IndexPath) {
        chapterIdClosure?(chapterArr[indexPath.item].chapterId.intValue)
    }
}

extension WNChapterVC {
    
    func getChapterListRequest() {
        provider_book.rx.request(.chapterList(baseId))
            .asObservable().mapModel(BaseModel<ChapterListModel>.self)
            .subscribe(onNext: { [weak self] (model) in
                guard model.code == 0 else {
                    self?.wn_toast(model.msg ?? "")
                    return
                }
                if let data = model.data, let list = data.list, list.count > 0 {
                    self?.bookDetailModel = data.bookDetial
                    self?.chapterArr = list
                    self?.chapterTabView.reloadData()
                    var item = 0
                    for (i, v) in list.enumerated() {
                        if v.chapterId.intValue == data.bookDetial?.durChapterId {
                            item = i
                        }
                    }
                    self?.chapterTabView.scrollToRow(at: IndexPath.init(row: item, section: 0), at: .middle, animated: true)
                }else {
                    self?.wn_toast(model.msg ?? "")
                }
            }, onError: { [weak self] (error) in
                self?.wn_toast("檢查網路連線")
            }).disposed(by: rx.disposeBag)
    }
}
